﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace mrp16062010.Veri
{
    class UrunTuru : ConnectionImporter
    {
        private int urunTuruId;

        public int UrunTuruId
        {
            get { return urunTuruId; }
            set { urunTuruId = value; }
        }

        private string ad;

        public string Ad
        {
            get { return ad; }
            set { ad = value; }
        }

        public void Kaydet()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("InsertUrunTuru", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmReturnValue = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
            prmReturnValue.Direction = ParameterDirection.ReturnValue;

            #region Populate Parameters
            SqlParameter prmAd = cmd.Parameters.Add("@Ad", SqlDbType.NVarChar);
            prmAd.Direction = ParameterDirection.Input;
            prmAd.Size = 50;

            prmAd.Value = this.Ad;


            SqlParameter prmUrunTuruId = cmd.Parameters.Add("@UrunTuruId", SqlDbType.Int);


            prmUrunTuruId.Direction = ParameterDirection.Output;

            #endregion

            cmd.ExecuteNonQuery();
            this.UrunTuruId = (int)prmUrunTuruId.Value;
            cmd.Dispose();
            Disconnect();
        }

        public void Sil()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("DeleteUrunTuru", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmUrunTuruId = cmd.Parameters.Add("@UrunTuruId", SqlDbType.Int);
            prmUrunTuruId.Direction = ParameterDirection.Input;
            prmUrunTuruId.Value = this.UrunTuruId;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Guncelle()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("UpdateUrunTuru", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmUrunTuruId = cmd.Parameters.Add("@UrunTuruId", SqlDbType.Int);
            prmUrunTuruId.Direction = ParameterDirection.Input;

            prmUrunTuruId.Value = this.UrunTuruId;


            SqlParameter prmAd = cmd.Parameters.Add("@Ad", SqlDbType.NVarChar);
            prmAd.Direction = ParameterDirection.Input;
            prmAd.Size = 50;

            prmAd.Value = this.Ad;


            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Disconnect();
        }

        public void Bul()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectUrunTuru", Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmUrunTuruId = cmd.Parameters.Add("@UrunTuruId", SqlDbType.Int);
            prmUrunTuruId.Direction = ParameterDirection.Input;
            prmUrunTuruId.Value = this.UrunTuruId;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                this.UrunTuruId = int.Parse(row["UrunTuruId"].ToString());
                this.ad = row["Ad"].ToString();
                break;
            }

            cmd.Dispose();
            Disconnect();
        }

        public List<UrunTuru> Listele()
        {
            Connect();
            SqlCommand cmd = new SqlCommand("SelectUrunTurusAll", Connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List<UrunTuru> liste = new List<UrunTuru>();
            foreach (DataRow row in dt.Rows)
            {
                UrunTuru yeniUrunTuru = new UrunTuru();
                yeniUrunTuru.UrunTuruId = int.Parse(row["UrunTuruId"].ToString());
                yeniUrunTuru.ad = row["Ad"].ToString();
                liste.Add(yeniUrunTuru);
            }

            cmd.Dispose();
            Disconnect();
            return liste;
        }
    }
}